/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.client;

import nfse.gui.config.ConfigService;
import nfse.gui.config.ConfigServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.*;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Window;  
import com.gwtext.client.widgets.Button;  
import com.gwtext.client.widgets.event.ButtonListenerAdapter;  
import com.gwtext.client.widgets.MessageBox;  

public class SetPasswordDialogBox extends Window {

    final PasswordTextBox oldTextBox = new PasswordTextBox();

    final PasswordTextBox newTextBox1 = new PasswordTextBox();

    final PasswordTextBox newTextBox2 = new PasswordTextBox();

    boolean cancelled = false;

    public SetPasswordDialogBox() {
        final SetPasswordDialogBox note = this;
        // Set the dialog box's caption.
        FlexTable table = new FlexTable();
        // DialogBox is a SimplePanel, so you have to set it's widget property
        // to
        // whatever you want its contents to be.
        
        this.setShadow(true);
        this.setFrame(true);
        this.setModal(true);
        
        this.setTitle("Set Password");
        this.setIconCls("pac-icon");

        int idx = -1;

        table.setHTML(++idx, 0, "<b>Old Password:</b>");

        table.setWidget(idx, 1, oldTextBox);
        table.setHTML(++idx, 0, "<b>New Password:</b>");

        table.setWidget(idx, 1, newTextBox1);
        table.setHTML(++idx, 0, "<b>Confirm:</b>");

        table.setWidget(idx, 1, newTextBox2);

        HorizontalPanel buttons = new HorizontalPanel();

        Button cancelButton = new Button();
        cancelButton.setText("Cancel");
        cancelButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                cancelled = true;
                note.hide();
            }
        });

        Button okButton = new Button();
        okButton.setText("OK");
        okButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                if (!newTextBox1.getText().equals(newTextBox2.getText())) {
                    MessageBox.alert("The new passwords do not match. Please try again.");
                    return;
                }
                setPassword(newTextBox1.getText(), oldTextBox.getText());
                cancelled = false;
                note.hide();
            }
        });

        this.addButton(okButton);
        this.addButton(cancelButton);

        table.setWidget(++idx, 0, buttons);
        this.add(table);

    }
    
    private void setPassword(String newPassword, String oldPassword) {
        // if (true)
        // return;

        ConfigServiceAsync nfseService = (ConfigServiceAsync) GWT.create(ConfigService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.CONFIG_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                NetFSEClient.redirect("NetFSEClient.html");
            }

            public void onSuccess(Object result) {
                NetFSEClient.showGenericDialog("Password Set", (String) result);
            }
        };

        nfseService.setPasswordUser(newPassword, oldPassword, callback);
    }

    public String getOldPassword() {
        return oldTextBox.getText();
    }

    public String getNewPassword() {
        return newTextBox1.getText();
    }

    public boolean isCancelled() {
        return cancelled;
    }
    
    public void reset() {
        oldTextBox.setText("");
        newTextBox1.setText("");
        newTextBox2.setText("");
    }

}
